package com.sgb.zhaocai.viewmodel;

import android.content.Context;

import androidx.lifecycle.ViewModel;

import com.sgb.zhaocai.base.BaseApi;
import com.sgb.zhaocai.entity.LabourWorkListEntity;
import com.sgb.zhaocai.entity.LeaseNeedListEntity;
import com.sgb.zhaocai.entity.MaterialListEntity;
import com.sgb.zhaocai.entity.ProjectListEntity;
import com.sgb.zhaocai.entity.RequestBean;
import com.sgb.zhaocai.entity.SubpackagePlanListEntity;
import com.sgb.zhaocai.view.demand.activity.DemandForecastActivity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Time:2021/8/31
 * AUTHOR:F_Q
 * Description:
 */
public class DemandForecastActModel extends ViewModel {

    private boolean isLastPage;

    /**
     * 获取列表信息
     * @param activity
     * @param position
     * @param requestBean
     */
    public void getMaterialHomeList(DemandForecastActivity activity, int position, RequestBean requestBean) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        switch (position) {
            case 0://工程项目
                BaseApi.getApi().getProjectList(requestBean).enqueue(new ICallBack<BaseEntity<ListResponse<List<ProjectListEntity>>>>() {
                    @Override
                    public void onSuccess(BaseEntity<ListResponse<List<ProjectListEntity>>> data) {
                        DialogHelper.dismissProgressDialog();
                        if (data.getState().equals(BaseApi.RESPONSE_OK)){
                            isLastPage = data.getData().getCurrentPage() >= data.getData().getTotal();
                            activity.setProjectList(data.getData().getList(),isLastPage);
                        }else {
                            MToast.showToast(activity,data.getMsg());
                        }
                    }
                });
                break;
            case 1://材料采购
                BaseApi.getApi().getMaterialList(requestBean).enqueue(new ICallBack<BaseEntity<ListResponse<List<MaterialListEntity>>>>() {
                    @Override
                    public void onSuccess(BaseEntity<ListResponse<List<MaterialListEntity>>> data) {
                        DialogHelper.dismissProgressDialog();
                        isLastPage = data.getData().getCurrentPage() >= data.getData().getTotal();
                        activity.setMaterialList(data.getData().getList(),isLastPage);
                    }
                });
                break;
            case 2://机械
                BaseApi.getApi().getLeaseNeedList(requestBean).enqueue(new ICallBack<BaseEntity<ListResponse<List<LeaseNeedListEntity>>>>() {
                    @Override
                    public void onSuccess(BaseEntity<ListResponse<List<LeaseNeedListEntity>>> data) {
                        DialogHelper.dismissProgressDialog();
                        isLastPage = data.getData().getCurrentPage() >= data.getData().getTotal();
                        activity.setLeaseNeedList(data.getData().getList(),isLastPage);
                    }
                });
                break;
            case 3://设备
                BaseApi.getApi().getEquipmentList(requestBean).enqueue(new ICallBack<BaseEntity<ListResponse<List<LeaseNeedListEntity>>>>() {
                    @Override
                    public void onSuccess(BaseEntity<ListResponse<List<LeaseNeedListEntity>>> data) {
                        DialogHelper.dismissProgressDialog();
                        isLastPage = data.getData().getCurrentPage() >= data.getData().getTotal();
                        activity.setLeaseNeedList(data.getData().getList(),isLastPage);
                    }
                });
                break;
            case 4://劳务
                BaseApi.getApi().getLabourWorkList(requestBean).enqueue(new ICallBack<BaseEntity<ListResponse<List<LabourWorkListEntity>>>>() {
                    @Override
                    public void onSuccess(BaseEntity<ListResponse<List<LabourWorkListEntity>>> data) {
                        DialogHelper.dismissProgressDialog();
                        isLastPage = data.getData().getCurrentPage() >= data.getData().getTotal();
                        activity.setLabourWorkList(data.getData().getList(),isLastPage);
                    }
                });
                break;
            case 5://分包
                BaseApi.getApi().getSubpackagePlanList(requestBean).enqueue(new ICallBack<BaseEntity<ListResponse<List<SubpackagePlanListEntity>>>>() {
                    @Override
                    public void onSuccess(BaseEntity<ListResponse<List<SubpackagePlanListEntity>>> data) {
                        DialogHelper.dismissProgressDialog();
                        isLastPage = data.getData().getCurrentPage() >= data.getData().getTotal();
                        activity.setSubpackagePlanList(data.getData().getList(),isLastPage);
                    }
                });
                break;
        }
    }

    /**
     * 跳转详情
     * @param context
     * @param id
     * @param longitude
     * @param latitude
     * @param textType
     */
    public void getH5Url(Context context, String id, String longitude, String latitude, String textType) {
        DialogHelper.showProgressDialog(context, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getUrlSetting(textType).enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    String webUrl = data.getData().getUrl() + "?id=" + id + "&lng=" + longitude + "&lat=" + latitude;
                    BaseApi.startWeb(context, webUrl);
                }else {
                    MToast.showToast(context,data.getMsg());
                }
            }
        });
    }

    /**
     * 地区
     * @param activity
     */
    public void getAddressModel(DemandForecastActivity activity) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    activity.setAddressList(data.getData());
                }
            }
        });
    }

    /**
     * 综合排序
     *
     * @param activity
     * @param position
     */
    public void getSortType(DemandForecastActivity activity, int position) {
        List<CommonPopEntity> datas = new ArrayList<>();
        switch (position){
            case 0:
                datas.clear();
                datas.add(new CommonPopEntity("1", "综合排序"));
                datas.add(new CommonPopEntity("3", "创建时间↓"));
                datas.add(new CommonPopEntity("2", "创建时间↑"));
                datas.add(new CommonPopEntity("4", "距离↓"));
                datas.add(new CommonPopEntity("6", "项目造价↓"));
                datas.add(new CommonPopEntity("5", "项目造价↑"));
                break;
            case 1:
            case 2:
                datas.clear();
                datas.add(new CommonPopEntity("1", "综合排序"));
                datas.add(new CommonPopEntity("3", "使用时间↓"));
                datas.add(new CommonPopEntity("2", "使用时间↑"));
                datas.add(new CommonPopEntity("4", "距离↓"));
                break;
            case 3:
            case 4:
                datas.clear();
                datas.add(new CommonPopEntity("1", "综合排序"));
                datas.add(new CommonPopEntity("3", "开工时间↓"));
                datas.add(new CommonPopEntity("2", "开工时间↑"));
                datas.add(new CommonPopEntity("4", "距离↓"));
                break;
        }
        activity.setSortTypeList(datas);

    }

    /**
     * 工程类型
     *
     * @param activity
     */
    public void getProjectTypeList(DemandForecastActivity activity) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getFindEngineeringTypeSelect().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    activity.setProjectTypeList(data.getData());
                } else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

    /**
     * 创建时间、开工时间
     *
     * @param activity
     */
    public void getTime(DemandForecastActivity activity) {
        BaseApi.getApi().getfindProjectTimeSelect().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    activity.setTime(data.getData());
                } else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

    /**
     * 材料类型
     * @param activity
     * CommonPopEntity
     */
    public void getMaterialType(DemandForecastActivity activity) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getGoodsType("").enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    activity.setMaterialType(data.getData());
                } else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

    /**
     * 机械类型
     * @param activity
     * CommonPopEntity
     */
    public void getMechanicalType(DemandForecastActivity activity) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getMachineOutTypeList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    activity.setMechanicalType(data.getData());
                } else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

    /**
     * 设备类型
     * @param activity
     * CommonPopEntity
     */
    public void getEquipmentType(DemandForecastActivity activity) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getDeviceTypeList("").enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)) {
                    activity.setEquipmentType(data.getData());
                } else {
                    MToast.showToast(activity, data.getMsg());
                }
            }
        });
    }

}
